﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SmokinAces.Object_Classes.Objects;
using SmokinAces.Object_Classes;
using SmokinAces;

namespace SmokinEditor
{
    public partial class Map_Creator : Form
    {
        Game_Editor parent;

        public Map_Creator(Game_Editor par)
        {
            parent = par;
            InitializeComponent();
        }

        private void Map_Creator_Load(object sender, EventArgs e)
        {
            CTileType.Items.Clear();
            Array EnumList = Enum.GetValues(typeof(Tile_Type));

            foreach (object iter in EnumList)
            {
                CTileType.Items.Add(iter);
            }
            CTileType.SelectedItem = EnumList.GetValue(0);
        }
        //This creates the grass etc.
        private void button1_Click(object sender, EventArgs e)
        {
            int XTiles;
            int YTiles;
            if (!Int32.TryParse(Tile_X.Text,out XTiles))
                return;
            if (!Int32.TryParse(Tile_Y.Text, out YTiles))
                return;



            const int divisor = 100;
            Random rand = new Random();
            List<Game_Object> MapTiles = new List<Game_Object>();
            int numTilesY = 64; // viewHeight / divisor; //so we have exactly 32 tiles across
            int numTilesX = 64; // viewWidth / divisor; //so we have exactly 32 tiles down
            int[] colorvertices = new int[1088];
            
            switch(((Tile_Type)CTileType.SelectedItem))
            {
                case Tile_Type.Grass:
                    for (int i = 0; i < 1088; i += 2)
                    {
                        colorvertices[i] = rand.Next(0, Map_Colors.GrassColorList.Length);
                        colorvertices[i + 1] = colorvertices[i];
                    }
                    break;
                case Tile_Type.Sand:
                    for (int i = 0; i < 1088; i += 2)
                    {
                        colorvertices[i] = rand.Next(0, Map_Colors.SandColorList.Length);
                        colorvertices[i + 1] = colorvertices[i];
                    }
                    break;
                case Tile_Type.Snow:
                    for (int i = 0; i < 1088; i += 2)
                    {
                        colorvertices[i] = rand.Next(0, Map_Colors.SnowColorList.Length);
                        colorvertices[i + 1] = colorvertices[i];
                    }
                    break;
                case Tile_Type.Swamp:
                    for (int i = 0; i < 1088; i += 2)
                    {
                        colorvertices[i] = rand.Next(0, Map_Colors.SwampColorList.Length);
                        colorvertices[i + 1] = colorvertices[i];
                    }
                    break;
                case Tile_Type.Water:
                    for (int i = 0; i < 1088; i += 2)
                    {
                        colorvertices[i] = rand.Next(0, Map_Colors.WaterColorList.Length);
                        colorvertices[i + 1] = colorvertices[i];
                    }
                    break;
            }

            int prevI = 0;
            int prevJ = 0;

            for (int j = 0; j <= XTiles; j++)
            {
                for (int i = 0; i <= YTiles; i++)
                {
                    int[] CornerColors = new int[4];
                    CornerColors[0] = colorvertices[Math.Abs(prevI + (prevJ * divisor)) % 1088]; //UL
                    CornerColors[1] = colorvertices[Math.Abs(i + (prevJ * divisor)) % 1088];//UR
                    CornerColors[2] = colorvertices[Math.Abs(prevI + (j * divisor)) % 1088];//BL
                    CornerColors[3] = colorvertices[Math.Abs(i + (j * divisor)) % 1088];//BR
                    Map_Tile tempTile = new Map_Tile(prevI * numTilesX, prevJ * numTilesY, Weather_Type.None, Enviornment_Type.Normal, ((Tile_Type)CTileType.SelectedItem), CornerColors, parent);
                    MapTiles.Add(tempTile);
                    prevI = i;
                }
                prevI = 0;
                prevJ = j;
            }
            
            parent.NewMap(MapTiles, XTiles * 64, YTiles * 64);
            this.Close();
            parent.FormHandle.Activate();
            
        }
    }

}

